#include "base_widget.h"
#include "ui_base_widget.h"

BaseWidget::BaseWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::BaseWidget)
{
    ui->setupUi(this);

    header = new HeaderWidget(this);
    header->setMaximumHeight(40);
    home = new HomeWidget(this);
    home->setVisible(true);
    graphic = new GraphicWidget(this);
    graphic->setVisible(false);
    manage = new ManageWidget(this);
    manage->setVisible(false);
    misc = new MiscWidget(this);
    misc->setVisible(false);

    mainLayout = new QVBoxLayout(this);
    mainLayout->setSpacing(10);
    mainLayout->addWidget(header);
    mainLayout->addWidget(home);
    mainLayout->addWidget(graphic);
    mainLayout->addWidget(manage);
    mainLayout->addWidget(misc);

    connect(header, &HeaderWidget::operateChange, this, &BaseWidget::slot_operateChange);

//    ThreadSerialPort::serialParameter par = {
//        .name = "",
//        .baud = QSerialPort::Baud9600,
//        .datab = QSerialPort::Data8,
//        .stopb = QSerialPort::OneStop,
//        .parity = QSerialPort::NoParity,
//    };
//    m_thdser = new ThreadSerialPort(par, this);
//    connect(m_thdser, SIGNAL(readData(std::shared_ptr<QByteArray>)),
//            this, SLOT(slot_readData(std::shared_ptr<QByteArray>)));
//    connect(m_thdser, SIGNAL(connected()), this, SLOT(slot_ser_connected()));
//    m_thdser->start();
}

BaseWidget::~BaseWidget()
{
    delete ui;

    m_thdser->quit();
    m_thdser->wait();
}

void BaseWidget::slot_operateChange(HeaderWidget::OperateType type) {
    switch (type) {
    case HeaderWidget::OT_PAGE1:
        home->setVisible(true);
        graphic->setVisible(false);
        manage->setVisible(false);
        misc->setVisible(false);
        break;
    case HeaderWidget::OT_PAGE2:
        home->setVisible(false);
        graphic->setVisible(true);
        manage->setVisible(false);
        misc->setVisible(false);
        break;
    case HeaderWidget::OT_PAGE3:
        home->setVisible(false);
        graphic->setVisible(false);
        manage->setVisible(true);
        misc->setVisible(false);
        break;
    case HeaderWidget::OT_PAGE4:
        home->setVisible(false);
        graphic->setVisible(false);
        manage->setVisible(false);
        misc->setVisible(true);
        break;
    default:
        break;
    }
}

void BaseWidget::slot_thrdser_connected()
{

}

void BaseWidget::slot_thrdser_readdata(std::shared_ptr<QByteArray> sba)
{
    emit thrdser_recieve(sba);
}

void BaseWidget::slot_thrdser_send(std::shared_ptr<QByteArray> sba)
{
    m_thdser->Send(sba);
}
